﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace CalemShop
{
    public partial class FrmStock : Form
    {
        private DataTable itemType;

        Hashtable itemCache = new Hashtable();
        private ArrayList genderData;
        private int currentPage = 1;

        public FrmStock()
        {
            InitializeComponent();
            genderData = new ArrayList();
            genderData.Add(new Gender("Nam", "T"));
            genderData.Add(new Gender("Nữ", "G"));
            listGender.DataSource = genderData;
            listGender.DisplayMember = "GenderName";
            listGender.ValueMember = "GenderValue";
        }

        private void FrmStock_Load(object sender, EventArgs e)
        {
            loadItemIds();
            itemType = Database.getItemType();
            itemTypeSource.DataSource = itemType;
            bindingSource.DataSource = Database.getStock(1);
            listType.DataSource = itemType;
            listType.DisplayMember = "typeName";
            listType.ValueMember = "typeId";
            txtMaSo.DataBindings.Add("Text", bindingSource, "itemId", true, DataSourceUpdateMode.OnPropertyChanged);
            txtName.DataBindings.Add("Text", bindingSource, "itemName", true, DataSourceUpdateMode.OnPropertyChanged);
            txtAmount.DataBindings.Add("Text", bindingSource, "amount", true, DataSourceUpdateMode.OnPropertyChanged);
            txtDesc.DataBindings.Add("Text", bindingSource, "desc", true, DataSourceUpdateMode.OnPropertyChanged);
            txtCost.DataBindings.Add("Text", bindingSource, "cost", true, DataSourceUpdateMode.OnPropertyChanged);
            txtGiaSi.DataBindings.Add("Text", bindingSource, "giasi", true, DataSourceUpdateMode.OnPropertyChanged);
            txtPrice.DataBindings.Add("Text", bindingSource, "price", true, DataSourceUpdateMode.OnPropertyChanged);
            txtSize.DataBindings.Add("Text", bindingSource, "size", true, DataSourceUpdateMode.OnPropertyChanged);
            listType.DataBindings.Add("Text", bindingSource, "typeName", true, DataSourceUpdateMode.OnPropertyChanged);
            listType.DataBindings.Add("SelectedValue", bindingSource, "type", true, DataSourceUpdateMode.OnPropertyChanged);

            listColor.DataBindings.Add("Text", bindingSource, "color", true, DataSourceUpdateMode.OnPropertyChanged);
            long nextId = Database.getLastItemIdInStock() + 1;
            lbNextID.Text = "MS kế tiếp :" + nextId;

        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.S))
            {
                DataRowView rowView = bindingSource.Current as DataRowView;
                Database.saveStockRows(rowView.Row);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            DataRowView rowView = bindingSource.Current as DataRowView;
            DataRow row = rowView.Row;
            Database.saveStockRows(row);
            bindingSource.ResetBindings(true);
            FrmMain frmMain = (FrmMain)this.MdiParent;
            if (frmMain.frmSale != null)
            {
                frmMain.frmSale.initData();
            }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            DataRowView rowView = bindingSource.Current as DataRowView;
            DataRow row = rowView.Row;
            Database.deleteStockRow(row);
            rowView.Delete();
        }

        private void FrmStock_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                FrmMain parent = this.MdiParent as FrmMain;
                parent.frmStock = null;
            }
            catch (Exception)
            {
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            //FrmNewItem frmNewItem = new FrmNewItem();
            FrmInputStock frmNewItem = new FrmInputStock();
            frmNewItem.ShowDialog(this);
            //if (frmNewItem.isChangedData)
            {
                //frmNewItem.isChangedData = false;
                bindingSource.DataSource = Database.getStock(currentPage);
                long nextId = Database.getLastItemIdInStock() + 1;
                lbNextID.Text = "MS kế tiếp :" + nextId;
                FrmMain frmMain = (FrmMain)this.MdiParent;
                if (frmMain.frmSale != null)
                {
                    frmMain.frmSale.initData();
                }
            }
        }

        void loadItemIds()
        {
            cbMaSo.AutoCompleteCustomSource = Database.getAllItemIds();
            cbMaSoMoi.AutoCompleteCustomSource = Database.listItemAutoKeys;
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            string maso = cbMaSo.Text;
            btNext.Enabled = true;
            btPre.Enabled = true;
            if (string.IsNullOrEmpty(maso))
            {
                return;
            }

            maso = maso.Trim();
            bindingSource.DataSource = Database.searchItemInStock(maso);
            bindingSource.ResetBindings(true);
        }

        private void btImport_Click(object sender, EventArgs e)
        {
            FrmImportData frm = new FrmImportData();
            frm.ShowDialog(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            btNext.Enabled = true;
            btPre.Enabled = true;
            string maso = cbMaSoMoi.Text;
            if (string.IsNullOrEmpty(maso))
            {
                return;
            }

            maso = maso.Trim();
            bindingSource.DataSource = Database.searchItemInStockByAutoKey(maso);
            bindingSource.ResetBindings(true);
        }

        private void btNext_Click(object sender, EventArgs e)
        {
            int n = currentPage + 1;
            DataTable dt = Database.getStock(n);
            bindingSource.DataSource = dt;
            if (dt != null && dt.Rows.Count > 0)
            {
                currentPage = n;
            }
        }

        private void btPre_Click(object sender, EventArgs e)
        {
            int n = currentPage - 1;
            if (n < 0)
            {
                n = 0;
                currentPage = 0;
            }

            DataTable dt = Database.getStock(n);
            bindingSource.DataSource = dt;
            if (dt != null && dt.Rows.Count > 0)
            {
                currentPage = n;
            }
        }

        private void btCheckEmpty_Click(object sender, EventArgs e)
        {
            DataTable dt = Database.getStocWithItemEmpty();
            bindingSource.DataSource = dt;
            btNext.Enabled = false;
            btPre.Enabled = false;
        }

        private void txtSearchName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string keywork = txtSearchName.Text;
                btNext.Enabled = true;
                btPre.Enabled = true;
                if (string.IsNullOrEmpty(keywork))
                {
                    return;
                }

                bindingSource.DataSource = Database.searchItemInStockByName(keywork);
                bindingSource.ResetBindings(true);
            }
        }

        private void btTotalCost_Click(object sender, EventArgs e)
        {
            decimal total = Database.totalCostInStock();
            MessageBox.Show("Tổng giá trị hàng trong kho là : " + total.ToString("#,0 VNĐ"));
        }

    }
}
